﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StackExchange.Redis;
using vabp;

namespace Vabp.Infrastructure.Redis.Test
{
    public class RedisServiceTest:VabpTestBase<VabpInfrastructureRedisModule>
    {

        public IDatabase GetDatabase_Test(RedisDataBaseType dbType = RedisDataBaseType.User)
        {
            return RedisHelper.GetDatabase((int)dbType);
        }

        public string[] GetKeys_Test(RedisDataBaseType dbType = RedisDataBaseType.User)
        {
            return RedisHelper.GetServer().Keys(database: (int)dbType).Select(k => k.ToString()).ToArray();
        }

        public bool IsExsit_Test(string key, RedisDataBaseType dbIndex = RedisDataBaseType.User)
        {
            return RedisHelper.GetDatabase((int)RedisDataBaseType.User).KeyExists(key);
        }

        public string Remove_Test(string key, RedisDataBaseType dbType = RedisDataBaseType.User)
        {
            var database = RedisHelper.GetDatabase((int)dbType);
            var data = database.StringGet(key);

            database.KeyDelete(key);
            return data;

        }

        public string SetData(string key, string data, RedisDataBaseType dbType = RedisDataBaseType.User)
        {
            var database = RedisHelper.GetDatabase((int)dbType);

            string oldData = null;
            if (database.KeyExists(key))
            {
                oldData = database.StringGet(key);
            }

            database.StringSet(key, data);
            return oldData;
        }
    }
}
